package com.ikimuhendis.ldrawer;

import com.mikepenz.materialdrawer.interfaces.DrawerListener;
import com.mikepenz.materialdrawer.widget.MaterialDrawerSliderView;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.*;
import ohos.global.configuration.Configuration;

/**
 * ActionBarDrawerToggle
 *
 * @since 2021-07-12
 */
public class ActionBarDrawerToggle implements DrawerListener {
    private static final String TAG = ActionBarDrawerToggle.class.getName();
    private static final int INT_2 = 2;
    protected Ability mActivity;
    protected MaterialDrawerSliderView mDrawerLayout;
    protected String mOpenDrawerContentDescRes;
    protected String mCloseDrawerContentDescRes;
    protected DrawerArrowDrawable mDrawerImage;
    protected boolean isAnimateEnabled;

    /**
     * ActionBarDrawerToggle
     *
     */
    public ActionBarDrawerToggle() {
        super();
    }

    /**
     * ActionBarDrawerToggle
     *
     * @param activity
     * @param drawerLayout
     * @param drawerImage
     * @param openDrawerContentDescRes
     * @param closeDrawerContentDescRes
     */
    public ActionBarDrawerToggle(Ability activity, MaterialDrawerSliderView drawerLayout,
                                 DrawerArrowDrawable drawerImage, String openDrawerContentDescRes,
                                 String closeDrawerContentDescRes) {
        mActivity = activity;
        mDrawerLayout = drawerLayout;
        mOpenDrawerContentDescRes = openDrawerContentDescRes;
        mCloseDrawerContentDescRes = closeDrawerContentDescRes;
        mDrawerImage = drawerImage;
        isAnimateEnabled = true;
    }

    /**
     * syncState
     *
     */
    public void syncState() {
        if (mDrawerImage == null) {
            return;
        }
        if (isAnimateEnabled) {
            if (mDrawerLayout.isOpened()) {
                mDrawerImage.setProgress(1.f);
            } else {
                mDrawerImage.setProgress(0.f);
            }
        }
        setActionBarUpIndicator();
        setActionBarDescription();
    }

    /**
     * setDrawerIndicatorEnabled
     *
     * @param isEnable
     */
    public void setDrawerIndicatorEnabled(boolean isEnable) {
        if (mDrawerImage == null) {
            return;
        }
        setActionBarUpIndicator();
        setActionBarDescription();
    }

    /**
     * isDrawerIndicatorEnabled
     *
     * @return boolean
     */
    public boolean isDrawerIndicatorEnabled() {
        if (mDrawerImage == null) {
            return false;
        }
        return true;
    }

    /**
     * onConfigurationChanged
     *
     * @param newConfig
     */
    public void onConfigurationChanged(Configuration newConfig) {
        if (mDrawerImage == null) {
            return;
        }
        syncState();
    }

    @Override
    public void onDrawerClosed(Component component) {
        if (mDrawerImage == null) {
            return;
        }
        if (isAnimateEnabled) {
            mDrawerImage.setProgress(0.f);
        }
        setActionBarDescription();
    }

    @Override
    public void onDrawerOpened(Component component) {
        if (mDrawerImage == null) {
            return;
        }
        if (isAnimateEnabled) {
            mDrawerImage.setProgress(1.f);
        }
        setActionBarDescription();
    }

    @Override
    public void onDrawerSlide(Component component, float v) {
        if (mDrawerImage == null) {
            return;
        }
        if (isAnimateEnabled) {
            mDrawerImage.setVerticalMirror(!mDrawerLayout.isOpened());
            mDrawerImage.setProgress(v);
        }
    }

    @Override
    public void onDrawerStateChanged(int i) {
    }

    /**
     * setActionBarUpIndicator
     *
     */
    protected void setActionBarUpIndicator() {
        if (mActivity != null) {
            final Component home = mActivity.findComponentById(ResourceTable.Media_ic_drawer);
            if (home == null) {
                return;
            }

            final ComponentContainer parent = (ComponentContainer) home.getComponentParent();
            final int childCount = parent.getChildCount();
            if (childCount != INT_2) {
                return;
            }

            final Component first = parent.getComponentAt(0);
            final Component second = parent.getComponentAt(1);
            final Component up = first.getId() == ResourceTable.Media_ic_drawer ? second : first;

            if (up instanceof Image) {
                Image upV = (Image) up;
                upV.setImageElement(mDrawerImage);
            }
        }
    }

    /**
     * setActionBarDescription
     *
     */
    protected void setActionBarDescription() {
    }

    /**
     * setAnimateEnabled
     *
     * @param isEnabled
     */
    public void setAnimateEnabled(boolean isEnabled) {
        this.isAnimateEnabled = isEnabled;
    }

    /**
     * isAnimateEnabled
     *
     * @return boolean
     */
    public boolean isAnimateEnabled() {
        return this.isAnimateEnabled;
    }
}
